import { Component, OnInit, Input } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { _HttpClient } from '@delon/theme';
import { SFSchema, SFUISchema, SFButton } from '@delon/form';
import { DictService } from '@core';
import { map } from 'rxjs/operators';
import { environment } from '@env/environment';

@Component({
  selector: 'app-sys-resource-edit',
  templateUrl: './edit.component.html',
})
export class SysResourceEditComponent implements OnInit {
  record: any = {};
  @Input() i: any;
  schema: SFSchema = {
    properties: {
      code: { type: 'string', title: '代码', maxLength: 30 },
      name: { type: 'string', title: '名称', maxLength: 30 },
      httpMethod: { type: 'string', title: '请求方法', maxLength: 30 },
      link: { type: 'string', title: '链接', maxLength: 100 },
      icon: { type: 'string', title: '图标', maxLength: 30 },
      target: { type: 'string', title: '打开方式', default: '_self' },
      i18n: { type: 'string', title: '国际化', maxLength: 30 },
      sort: { type: 'integer', title: '排序' },
      type: { type: 'string', title: '类型' },
      serviceId: { type: 'string', title: '服务ID' },
      enabled: {
        type: 'string',
        title: '是否有效',
        enum: [
          { label: '是', value: 1 },
          { label: '否', value: 0 },
        ],
        default: 1,
      },
      roleIds: { type: 'string', title: '角色', maxLength: 15 },
    },
    required: ['code', 'name', 'type', 'roleIds'],
  };
  ui: SFUISchema = {
    '*': {
      spanLabelFixed: 100,
      grid: { span: 12 },
    },
    $target: {
      widget: 'radio',
      asyncData: () => this.dictService.getSFSchemaEnumTypes('TARGET'),
      default: 'N',
    },
    $type: {
      widget: 'radio',
      asyncData: () => this.dictService.getSFSchemaEnumTypes('RESOURCE_TYPE'),
      default: 'N',
    },
    $httpMethod: {
      widget: 'radio',
      asyncData: () => this.dictService.getSFSchemaEnumTypes('HTTP_METHOD'),
      default: 'N',
    },
    $serviceId: {
      widget: 'radio',
      asyncData: () => this.dictService.getSFSchemaEnumTypes('SERVICE_ID'),
      default: 'N',
    },
    $enabled: {
      widget: 'radio',
      default: 'Y',
    },
    $roleIds: {
      grid: { span: 24 },
      widget: 'transfer',
      titles: ['未拥有', '已拥有'],
      asyncData: () =>
        this.http.get(`${environment.sys.ctx}/sysRole/findAll`).pipe(
          map(items => {
            const options: any[] = [];
            items.forEach((element: any) => {
              options.push({ title: element.name, value: element.id });
            });
            return options;
          }),
        ),
    },
  };

  button: SFButton = {
    submit: '保存',
  };

  constructor(private msgSrv: NzMessageService, public http: _HttpClient, private dictService: DictService) {}

  ngOnInit(): void {}

  save(value: any) {
    value.roles = [];
    value.roleIds.forEach((roleId: any) => value.roles.push({ id: roleId }));
    this.http.put(`${environment.sys.ctx}/sysResource`, value).subscribe(res => {
      this.msgSrv.success('保存成功');
    });
  }
}
